package com.taptech.doufu.ui.dialog;

import android.app.DialogFragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import com.taptech.doufu.util.TimeUtil;
import com.taptech.doufu.R;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.ui.fragment.SelectDateFragmet;
import com.taptech.doufu.ui.fragment.SelectTimeFragmet;
import com.taptech.doufu.util.UIUtil;

/**
 * Created by Kelly Li on 2016-12-03.
 */

public class NovelCreateSentTimeDialog extends DialogFragment implements View.OnClickListener {

    SelectDateFragmet dateFragmet;
    SelectTimeFragmet timeFragmet;
    private  SureSentListener sureSentListener;
    private TextView timeDesTv, timeBtn, dateBtn, sureSentBtn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.novel_create_sent_time_dialog, null);
        initView(view);
        return view;
    }

    private void initView(View contentView) {
        timeDesTv = (TextView) contentView.findViewById(R.id.sent_time_dialog_timedes_tv);
        dateBtn = (TextView) contentView.findViewById(R.id.sent_time_dialog_date_tv);
        dateBtn.setOnClickListener(this);
        timeBtn = (TextView) contentView.findViewById(R.id.sent_time_dialog_time_tv);
        timeBtn.setOnClickListener(this);
        sureSentBtn = (TextView) contentView.findViewById(R.id.sent_time_dialog_sure_btn);
        sureSentBtn.setOnClickListener(this);
        contentView.findViewById(R.id.novel_create_sent_time_space_layout).setOnClickListener(this);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Window window = getDialog().getWindow();
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        changeFragment(0);
    }

    private void changeFragment(int type) {
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        switch (type) {
            case 0:
                if (dateFragmet == null) {
                    dateFragmet = new SelectDateFragmet();
                    dateFragmet.setChangeDate(changeDate);
                    fragmentTransaction.add(R.id.sent_time_dialog_time_fglayout, dateFragmet,"dateFragmet");
                }
                if(timeFragmet != null){
                    fragmentTransaction.hide(timeFragmet);
                }
                fragmentTransaction.show(dateFragmet).commit();
                break;

            case 1:
                if (timeFragmet == null) {
                    timeFragmet = new SelectTimeFragmet();
                    timeFragmet.setChangeDate(changeDate);
                    timeFragmet.setCurHour(hours);
                    fragmentTransaction.add(R.id.sent_time_dialog_time_fglayout,timeFragmet,"timeFragmet");
                }
                fragmentTransaction.hide(dateFragmet);
                fragmentTransaction.show(timeFragmet).commit();
                break;
        }


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sent_time_dialog_date_tv: //日期
                changeFragment(0);
                if(WeMediaApplication.getInstance().isDayNightMode_Night){
                    dateBtn .setBackgroundResource(R.drawable.btn_search_article_press_bg_dark);
                    dateBtn.setTextColor(getResources().getColor(R.color.text_dark));
                    timeBtn.setTextColor(getResources().getColor(R.color.theme_main_dark));
                }else {
                    dateBtn.setBackgroundResource(R.drawable.btn_search_article_press_bg);
                    dateBtn.setTextColor(getResources().getColor(R.color.white));
                    timeBtn.setTextColor(getResources().getColor(R.color.text_color_67));
                }
                timeBtn.setBackgroundColor(Color.parseColor("#00ffffff"));
                break;
            case R.id.sent_time_dialog_time_tv: //时间
                changeFragment(1);
                if(WeMediaApplication.getInstance().isDayNightMode_Night){
                    timeBtn.setBackgroundResource(R.drawable.btn_search_user_press_bg_dark);
                    timeBtn.setTextColor(getResources().getColor(R.color.text_dark));
                    dateBtn.setTextColor(getResources().getColor(R.color.theme_main_dark));
                }else {
                    timeBtn.setBackgroundResource(R.drawable.btn_search_user_press_bg);
                    timeBtn.setTextColor(getResources().getColor(R.color.white));
                    dateBtn.setTextColor(getResources().getColor(R.color.text_color_67));
                }
                dateBtn.setBackgroundColor(Color.parseColor("#00ffffff"));
                break;
            case R.id.sent_time_dialog_sure_btn: //确定发布
                sureSent();
                break;
            case R.id.novel_create_sent_time_space_layout:
                dismiss();
                break;
        }
    }
    private void sureSent(){
        String nowTime = TimeUtil.get24HourLaterTimeTo(TimeUtil.TIME_FORMAT);
        String selTime = dateStr + " " + String.format( "%02d",hours )+ ":"+String.format( "%02d",mins )+":00";
        try {
            boolean timeNomal = TimeUtil.compareTimeSize(nowTime ,selTime);
            if(timeNomal && sureSentListener != null){
                sureSentListener.onDate(selTime);
            }else {
                UIUtil.toastMessage(getActivity(), "定时时间须大于当前24小时");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    private String  dateStr = "";
    private int hours,mins;
    private ChangeDate changeDate = new ChangeDate() {
        @Override
        public void changeDateListener(String date) {
            dateStr = date;
            changeTimeDes();
        }

        @Override
        public void changeTimeListener(int hour,int min) {
            hours = hour;
            mins = min;
            changeTimeDes();
        }
    };

    private void changeTimeDes(){
        String hourStr = String.format( "%02d",hours )+ ":"+String.format( "%02d",mins );
        timeDesTv.setText(dateStr + " " + hourStr);
    }

    public void setSureSentListener(SureSentListener sureSentListener) {
        this.sureSentListener = sureSentListener;
    }

    public  interface  ChangeDate{
        void changeDateListener(String date);
        void changeTimeListener(int hour,int min);
    }

    public interface SureSentListener{
        void onDate(String date);
    }
}